package com.yhf.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhf.config.CreateDataBase;
import com.yhf.mapper.DeviceMapper;
import com.yhf.service.DeviceGatherService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class MyJob implements Job {
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    DeviceGatherService deviceGatherService;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        List<Integer> collect = devices.stream().map(Device::getId).collect(Collectors.toList());
        Random random = new Random();
        Integer integer = collect.get(random.nextInt(collect.size()));
        Device device = null;
        for (int i = 0;i<devices.size();i++) {
            if (devices.get(i).getId() == integer) {
                device = devices.get(i);
            }
        }
        DeviceGather deviceGather = new DeviceGather();
        deviceGather.setDeviceName(device.getName());
        deviceGather.setDevicesn(device.getSn());
        int i = random.nextInt(500);
        float i1 = i;
        deviceGather.setBasicData(i1);
        deviceGather.setGatherTime(new Date());
        deviceGatherService.save(deviceGather);
    }
}
